package com.silence.whereeat.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;

import java.io.InputStream;
import java.net.URL;

/**
 * Created by KIM on 2015/8/31.
 */
public class ImageDownload {
    private String IMG_PATH = "";

    public ImageDownload(String IMG_PATH) {
        this.IMG_PATH = IMG_PATH;
    }

    public void download(final ImageCallBack callBack) {
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                callBack.getBitMap((Bitmap) msg.obj);
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(IMG_PATH);
                    InputStream inputStream = url.openStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    Message message = Message.obtain();
                    message.obj = bitmap;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public interface ImageCallBack {
        void getBitMap(Bitmap bitmap);
    }
}
